var menuBar = null;
var files = '';

document.ready(function() {
    menuBar = getHtml('writeBar');
    setHtml('writeBar', '');

    queryKey('ok');
});

function queryKey(flag) {
    openLoad();
    resetPageBar(flag);

    var param = {};
    param.ctrl = 'account';
    param.func = 'getMenuList';
    param.isuse = getAjaxValue('isuse');
    param.menutype = getAjaxValue('menutype0');
    param.menugrade = getAjaxValue('menugrade0');
    param.useto = getAjaxValue('useto');
    param.keyword = getAjaxValue('keyword');
    param.page = getAjaxValue('page');

    sendAjax(param, function(result) {
        menuList.list = result.menuList;

        loadPageBar(getValue('page'), result.allPages, result.rowCount);
        setHtml('listCount', result.listCount);

        setDisplay('mainBg', '');
        closeLoad();
    });
}

function addMenu() {
    setValue('tempid', '');
    setValue('flag', 'add');
    openFloat2('600px', '70px', lyFloatLeft, menuBar);
    setHtml('writeTitle', '添加菜单');
    setUpload();
}

function closeWriteDiv() {
    closeFloat();
}

function setUpload() {
    files = '';
    layui.use('upload', function() {
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#selectImg', 
            accept: 'images', 
            acceptMime: 'image/*', 
            auto: false, 
            size: 30, 
            choose: function(obj) {
                obj.preview(function(index, file, result) {
                    getItem('menupic').src = result;
                    files = result;
                });
            }, 
        });
    });
}

function setOperate(id, name) {
    openSlipIframeDiv('admin.php?m=account&c=operate&menuid=' + id + '&menu=' + escape(name));
}

function saveMenuInfo(obj) {
    if (isEmpty('menumc') || isWicked('menumc')) {
        showTip('菜单名称格式错误');
        return;
    }
    if (isEmpty('menusx') || !isNum('menusx')) {
        showTip('顺序格式错误');
        return;
    }
    if (isEmpty('menutype') || (getValue('menutype') != '0' && getValue('menutype') != '1')) {
        showTip('类型格式错误');
        return;
    }
    if (isEmpty('menugrade') || (getValue('menugrade') != '0' && getValue('menugrade') != '1')) {
        showTip('等级格式错误');
        return;
    }
    if (getValue('menutype') == '0' && getValue('menugrade') == '1') {
        if (isEmpty('menumode') || (getValue('menumode') != 'blank' && getValue('menumode') != 'link')) {
            showTip('打开方式格式错误');
            return;
        }
        if (isEmpty('menuurl')) {
            showTip('链接不能为空');
            return;
        }
    }
    else if (getValue('menutype') == '1' && getValue('menugrade') == '0') {
        if (isEmpty('menuparent')) {
            showTip('请选择上一级菜单');
            return;
        }
        if (files == '') {
            showTip('请上传图片');
            return;
        }
    }
    else if (getValue('menutype') == '1' && getValue('menugrade') == '1') {
        if (isEmpty('menuparent')) {
            showTip('请选择上一级菜单');
            return;
        }
        if (isEmpty('menumode') || (getValue('menumode') != 'blank' && getValue('menumode') != 'link')) {
            showTip('打开方式格式错误');
            return;
        }
        if (isEmpty('menuurl')) {
            showTip('链接不能为空');
            return;
        }
        if (files == '') {
            showTip('请上传图片');
            return;
        }
    }

    obj.style.pointerEvents = 'none';

    var param = {};
    param.ctrl = 'account';
    param.func = 'saveMenu';
    param.flag = getAjaxValue('flag');
    param.menuid = getAjaxValue('tempid');
    param.menuparent = getAjaxValue('menuparent');
    param.menumc = getAjaxValue('menumc');
    param.menugrade = getAjaxValue('menugrade');
    param.menutype = getAjaxValue('menutype');
    param.menumode = getAjaxValue('menumode');
    param.picText = encodeURIComponent(files);
    param.menuurl = getAjaxValue('menuurl');
    param.useto = getItemByName('menuuseto').checked ? '0' : '1';
    param.status = getItemByName('menustatus').checked ? '1' : '0';
    param.menusx = getAjaxValue('menusx');

    sendAjax(param, function(result) {
        closeFloat();
        queryKey('ok');
    });
}

function editMenu(id) {
    setValue('tempid', id);
    setValue('flag', 'edit');

    var param = {};
    param.ctrl = 'account';
    param.func = 'getMenuInfo';
    param.menuid = getAjaxValue('tempid');

    sendAjax(param, function(result) {
        openFloat2('600px', '70px', lyFloatLeft, menuBar);
        setHtml('writeTitle', '编辑菜单');
        setUpload();

        if (result.type == '') {
            setMenuItemStatus('none', 'none', 'none', 'none', 'none');
        }
        else if (result.type == '0') {
            if (result.grade == '') {
                setMenuItemStatus('', 'none', 'none', 'none', 'none');
            }
            else if (result.grade == '0') {
                setMenuItemStatus('', 'none', 'none', 'none', 'none');
            }
            else {
                setMenuItemStatus('', 'none', '', '', 'none');
            }
        }
        else {
            if (result.grade == '') {
                setMenuItemStatus('', 'none', 'none', 'none', 'none');
            }
            else if (result.grade == '0') {
                setMenuItemStatus('', '', 'none', 'none', '');
            }
            else {
                setMenuItemStatus('', '', '', '', '');
            }
        }

        setHtml('menuparent', '');
        setSelectItem('menuparent', [{'text':'上一级', 'value':''}]);
        if (result.parentMenu != '') {
            setSelectItem('menuparent', result.parentMenu);
        }

        setValue('menumc', result.name);
        setValue('menusx', result.sx);
        setValue('menuurl', result.url);

        if (result.icon != '') {
            getItem('menupic').src = result.iconPath + 'upload/menu/' + result.icon;
            files = result.icon;
        }

        locationSelect('menutype', result.type);
        locationSelect('menugrade', result.grade);
        locationSelect('menuparent', result.parentid);
        locationSelect('menumode', result.mode);

        getItem('menutype').disabled = 'disabled';
        getItem('menugrade').disabled = 'disabled';
        getItem('menutype').style.backgroundColor = '#eee';
        getItem('menugrade').style.backgroundColor = '#eee';

        if (result.isuse == 1)
            getItemByName('menustatus', 0).checked = 'checked';
        else
            getItemByName('menustatus', 1).checked = 'checked';

        if (result.useto == 0)
            getItemByName('menuuseto', 0).checked = 'checked';
        else
            getItemByName('menuuseto', 1).checked = 'checked';
    });
}

function deleteMenu(id, name) {
    setValue('tempid', id);

    openConfirm('确定删除 【' + name + '】 吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'account';
        param.func = 'deleteMenu';
        param.menuid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            queryKey('page');
        });
    });
}

function setMenuItemStatus(grade, parent, mode, url, pic) {
    setDisplay('menugrade', grade);
    setDisplay('menuparent', parent);
    setDisplay('menumode', mode);
    setDisplay('menuurlBar', url);
    setDisplay('menupicBar', pic);

    if (grade == 'none') {
        getItem('menugrade').options[0].selected = true;
    }
    if (parent == 'none') {
        setHtml('menuparent', '');
        setSelectItem('menuparent', [{'text':'上一级', 'value':''}]);
        getItem('menuparent').options[0].selected = true;
    }
    if (mode == 'none') {
        getItem('menumode').options[0].selected = true;
    }
    if (url == 'none') {
        setValue('menuurl', '');
    }
    if (pic == 'none') {
        getItem('menupic').src = '';
        files = '';
    }
}

function selectMenutype() {
    if (getValue('menutype') == '') {
        setMenuItemStatus('none', 'none', 'none', 'none', 'none');
    }
    else if (getValue('menutype') == '0') {
        if (getValue('menugrade') == '') {
            setMenuItemStatus('', 'none', 'none', 'none', 'none');
        }
        else if (getValue('menugrade') == '0') {
            setMenuItemStatus('', 'none', 'none', 'none', 'none');
        }
        else {
            setMenuItemStatus('', 'none', '', '', 'none');
        }
    }
    else {
        if (getValue('menugrade') == '') {
            setMenuItemStatus('', 'none', 'none', 'none', 'none');
        }
        else if (getValue('menugrade') == '0') {
            setMenuItemStatus('', '', 'none', 'none', '');

            var param = {};
            param.ctrl = 'account';
            param.func = 'getTopParentMenu';

            sendAjax(param, function(result) {
                setHtml('menuparent', '');
                setSelectItem('menuparent', [{'text':'上一级', 'value':''}]);
                setSelectItem('menuparent', result.menuList);
            });
        }
        else {
            setMenuItemStatus('', '', '', '', '');
            
            var param = {};
            param.ctrl = 'account';
            param.func = 'getLeftParentMenu';

            sendAjax(param, function(result) {
                setHtml('menuparent', '');
                setSelectItem('menuparent', [{'text':'上一级', 'value':''}]);
                setSelectItem('menuparent', result.menuList);
            });
        }
    }
}

function selectMenugrade() {
    if (getValue('menugrade') == '') {
        setMenuItemStatus('', 'none', 'none', 'none', 'none');
    }
    else if (getValue('menugrade') == '0') {
        if (getValue('menutype') == '0') {
            setMenuItemStatus('', 'none', 'none', 'none', 'none');
        }
        else {
            setMenuItemStatus('', '', 'none', 'none', '');
            
            var param = {};
            param.ctrl = 'account';
            param.func = 'getTopParentMenu';

            sendAjax(param, function(result) {
                setHtml('menuparent', '');
                setSelectItem('menuparent', [{'text':'上一级', 'value':''}]);
                setSelectItem('menuparent', result.menuList);
            });
        }
    }
    else {
        if (getValue('menutype') == '0') {
            setMenuItemStatus('', 'none', '', '', 'none');
        }
        else {
            setMenuItemStatus('', '', '', '', '');
            
            var param = {};
            param.ctrl = 'account';
            param.func = 'getLeftParentMenu';

            sendAjax(param, function(result) {
                setHtml('menuparent', '');
                setSelectItem('menuparent', [{'text':'上一级', 'value':''}]);
                setSelectItem('menuparent', result.menuList);
            });
        }
    }
}